package gmu.xytxl.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import gmu.xytxl.dao.ILoginDAO;
import gmu.xytxl.util.JDBCUtils;

public class LoginDAOImpl implements ILoginDAO{

	@Override
	public int findAdmin(String username, String password) {
		int i = 0;
		String sql = "select * from admin where username = ?";
		List list = new ArrayList<>();
		list.add(username);
		ResultSet rs = JDBCUtils.findAll(sql, list);
		try {
			while (rs.next()) {
				System.out.println("1");
				if(password.equals(rs.getString("password")))
					i=1;
				else {
					i=2;
				}
			}
			
		} catch (SQLException e) {
			
			e.printStackTrace();
		}finally {
			JDBCUtils.closeConnection(rs, JDBCUtils.preparedStatement, JDBCUtils.conn);
			System.out.println(i);
			return i;
		}
		
	}

	@Override
	public String result(int i) {
		String string;
		switch (i) {
		case 0:
			string = "empty";
			break;
		case 1:
			string = "success";
			break;
		default:
			string = "err";
			break;
		}
		return string;
	}

}
